using Microsoft.AspNetCore.SignalR.Client;
using MultiAgentsClient.ChatDataStructures;
using MultiAgentsClient.Manager.DataModel;
using MultiAgentsShared;
using static MultiAgentsClient.Manager.AgentManager;

namespace MultiAgentsClient.Service.SignalRService;

public class SignalRAgentService : SignalRClientServiceBase
{
    private readonly SystemConfigDataModel systemConfigDataModel;
    private readonly UserDataModel userDataModel;

    public enum ApiRequestType
    {
        CreateNewAgentRequest,
        GenerateMessageRequest,
        UpdateAgentRequest,
        RemoveAgentRequest,
        ShareAgentRequest,
        LoadAgentByShareCodeRequest,
    }

    public SignalRAgentService(SystemConfigDataModel systemConfigDataModel, UserDataModel userDataModel) : base("AgentHub")
    {
        this.systemConfigDataModel = systemConfigDataModel;
        this.userDataModel = userDataModel;
    }

    protected override void HubMethodRegistration()
    {
        hubConnection.On<string>("CreateNewAgentResponse", CreateNewAgentResponse);
        hubConnection.On<string>("GenerateMessageResponse", GenerateMessageResponse);
        hubConnection.On<int>("RemoveAgentResponse", RemoveAgentResponse);
        hubConnection.On<string>("UpdateAgentResponse", UpdateAgentResponse);
    }

    private void CreateNewAgentResponse(string agentJson)
    {
        AgentTemplate agent = SerializableObjectBase.FromJson<AgentTemplate>(agentJson);
        eventBus.Publish(new CreateNewAgentResponseCommand(agent));
    }

    private void GenerateMessageResponse(string responseMessage)
    {
        eventBus.Publish(new GenerateMessageResponseCommand(responseMessage));
    }

    private void UpdateAgentResponse(string agentJson)
    {
        AgentTemplate agent = SerializableObjectBase.FromJson<AgentTemplate>(agentJson);
        eventBus.Publish(new UpdateAgentResponseCommand(agent));
    }

    private void RemoveAgentResponse(int AgentId)
    {
        eventBus.Publish(new RemoveAgentResponseCommand(AgentId));
    }
}
